<template>
  <div class="supplier-market">
    <el-main>
      <div class="max-w-1200px mx-auto px-20px">
        <div class="bread-box flex items-center justify-start">
          <div class="line-height-20px">当前位置：</div>
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>供方市场</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-card class="rounded-8px" shadow="never">
          <!-- 产品类型 Tab -->
          <div class="mb-20px">
            <el-row :gutter="20" class="flex items-start">
              <el-col :span="2">
                <h4>产品类型</h4>
              </el-col>
              <el-col :span="22">
                <el-tabs
                  v-model="productIndustryId"
                  type="card"
                  @tab-click="productIndustryChange"
                  class="custom-tabs flex-1"
                  style="white-space: normal; height: auto;"
                >
                  <el-tab-pane label="不限" name="-"></el-tab-pane>
                  <el-tab-pane
                    v-for="(value,index) in types"
                    :key="index"
                    :label="value.productTypeName"
                    :name="value.productTypeId"
                  ></el-tab-pane>
                </el-tabs>
              </el-col>
            </el-row>
          </div>

          <!-- 二级分类 Tab -->
          <div class="mb-20px" v-if="secondLevelTypes && secondLevelTypes.length > 0">
            <el-row :gutter="20" class="flex items-start">
              <el-col :span="2">
                <h4>二级类型</h4>
              </el-col>
              <el-col :span="22">
                <el-tabs
                  v-model="selectedSecondLevel"
                  type="card"
                  @tab-click="secondLevelChange"
                  class="custom-tabs flex-1"
                  style="white-space: normal; height: auto;"
                >
                  <el-tab-pane label="不限" name=""></el-tab-pane>
                  <el-tab-pane
                    v-for="(value,index) in secondLevelTypes"
                    :key="index"
                    :label="value.productTypeName"
                    :name="value.productTypeId"
                  ></el-tab-pane>
                </el-tabs>
              </el-col>
            </el-row>
          </div>

          <!-- 所属区域 Tab -->
          <div class=" mb-20px">
            <el-row :gutter="20" class="flex items-start">
              <el-col :span="2">
                <h4>所属区域</h4>
              </el-col>
              <el-col :span="22">
                <el-tabs
                  v-model="companyDivisionCode"
                  type="card"
                  @tab-click="companyDivisionChange"
                  class="custom-tabs flex-1"
                >
                  <el-tab-pane label="不限" name="-"></el-tab-pane>
                  <el-tab-pane
                    v-for="(value,index) in citys"
                    :key="index"
                    :label="value.prefecture"
                    :name="value.code"
                  ></el-tab-pane>
                </el-tabs>
              </el-col>
            </el-row>
          </div>
          <el-divider></el-divider>
          <!-- 搜索框 -->
          <div class="">
            <el-row :gutter="20" class="items-center flex">
              <el-col :span="2">
                <h4 class="line-height-40px m-0">关键字</h4>
              </el-col>
              <el-col :span="6">
                <el-input
                  v-model="searchKeyword"
                  placeholder="请输入关键字"
                  clearable
                  @keyup.enter.native="getListData"
                ></el-input>
              </el-col>
              <el-col :span="10">
                <el-button type="primary" @click="getListData" class="ml-10px">
                  搜索
                </el-button>
              </el-col>
            </el-row>
          </div>
        </el-card>

        <div class="mt-20px">
          <el-row :gutter="20" class="grid-container">
            <el-col
              :span="8"
              class="mb-20px"
              v-for="supplier in suppliers"
              :key="supplier.id"
            >
              <el-card
                class="flex flex-col h-full"
                shadow="hover"
                :body-style="{ padding: '0px' }"
                @click.native="handleSupplierClick(supplier)"
              >
                <div class="flex flex-col h-full">
                  <div class="flex flex-col items-center justify-center p-20px flex-grow">
                    <div class="flex items-center justify-center">
                      <img
                        :src="baseUrl+'api/public/file/v1/public/get/'+supplier.productImg"
                        class="w-120px h-120px object-cover m-0"
                      />
                    </div>
                    <div class="mt-15px w-full">
                      <p class="m-0 mb-5px font-bold product-name">{{ supplier.productName }}</p>
                    </div>
                    <div class="text-center mt-15px">
                      <el-tag
                        v-for="(value,index) in supplier.companyTypeList"
                        :key="index"
                        :type="getRandomTagType()"
                        class="mr-10px"
                      >
                        {{ value }}
                      </el-tag>
                    </div>
                  </div>
                  <div class="bg-[#0094de] text-white w-full text-center p-10px flex-shrink-0">
                    <h4 class="m-0 font-normal company-name">{{ supplier.companyName }}</h4>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage"
          @current-change="handlePageChange"
          class="flex justify-end"
        ></el-pagination>
      </div>
    </el-main>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getProductList,
  productTypeList,
  productTypeChildrenList,
} from "../api/home";
export default {
  name: "SupplierMarket",
  data() {
    return {
      productTypeId: "-",
      productIndustryId: "-",
      companyTypeId: "-",
      companyDivisionCode: "-",
      searchKeyword: "",
      suppliers: [],
      total: 100,
      currentPage: 1,
      pageSize: 9,
      baseUrl: process.env.VUE_APP_BASE_API,
      types: [],
      // 二级分类相关数据
      secondLevelTypes: [],
      selectedSecondLevel: "",
      currentProductTypeName: "请选择产品分类",
    };
  },
  computed: {
    ...mapGetters("dicts", ["cityDict", "getDictByParamsKey"]),
    citys() {
      return this.cityDict;
    },
    industrys() {
      return this.getDictByParamsKey({ parentId: 1 });
    },
    features() {
      return this.getDictByParamsKey({ parentId: 3 });
    },
  },
  mounted() {
    this.getData();
    this.getListData();
  },
  methods: {
    getData() {
      productTypeList().then((res) => {
        this.types = res.data;
      });
    },
    getListData() {
      const params = {
        isAsc: true,
        pageNo: this.currentPage,
        pageNum: this.pageSize,
        q: this.searchKeyword,
      };
      if (this.productTypeId != "-") {
        params.productTypeId = this.productTypeId;
      }
      if (this.productIndustryId != "-") {
        params.productIndustryId = this.productIndustryId;
      }
      // 添加二级分类参数
      if (this.selectedSecondLevel != "" && this.selectedSecondLevel != null) {
        params.secondLevelTypeId = this.selectedSecondLevel;
      }
      if (this.companyTypeId != "-") {
        params.companyTypeId = this.companyTypeId;
      }
      if (this.companyDivisionCode != "-") {
        params.companyDivisionCode = this.companyDivisionCode;
      }
      getProductList(params).then((res) => {
        this.suppliers = res.rows;
        this.total = res.total;
      });
    },
    productTypeChange(e) {
      this.productTypeId = e.name;
      this.getListData();
    },
    productIndustryChange(e) {
      this.productIndustryId = e.name;
      // 获取二级分类数据
      if (e.name !== "-") {
        productTypeChildrenList({ parentId: e.name }).then((res) => {
          this.secondLevelTypes = res.data;
          this.selectedSecondLevel = ""; // 重置二级分类选择
        });
      } else {
        // 如果选择的是"不限"，则清空二级分类数据
        this.secondLevelTypes = [];
        this.selectedSecondLevel = "";
      }
      this.getListData();
    },
    // 二级分类切换事件
    secondLevelChange(e) {
      this.selectedSecondLevel = e.name;
      this.getListData();
    },
    companyDivisionChange(e) {
      this.companyDivisionCode = e.name;
      this.getListData();
    },
    getRandomTagType() {
      const types = ["success", "warning", "danger"];
      const randomIndex = Math.floor(Math.random() * types.length);
      return types[randomIndex];
    },
    handleSupplierClick(supplier) {
      this.$router.push({
        name: "supplierMarketDetail",
        query: { id: supplier.productId },
      });
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.getListData();
    },
    // 根据ID获取产品类型名称
    getProductTypeNameById(id) {
      const type = this.types.find(item => item.productTypeId === id);
      return type ? type.productTypeName : '';
    }
  },
};
</script>

<style scoped>
.grid-container {
  display: flex;
  flex-wrap: wrap;
}

.product-name {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-all;
  line-height: 1.4;
  height: 45px;
}

.company-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.supplier-market {
  min-height: calc(100vh - 120px);
}
/* 自定义 Tabs 样式 */
.custom-tabs ::v-deep .el-tabs__header {
  margin-bottom: 0 !important;
  border-bottom: none !important;
}

.custom-tabs ::v-deep .el-tabs__nav {
  border: none !important;
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
}

.custom-tabs ::v-deep .el-tabs__item {
  height: 30px !important;
  line-height: 30px !important;
  padding: 0 12px !important;
  border: none !important;
  background: transparent !important;
  border-radius: 0 !important;
  flex-shrink: 0; /* 防止压缩 */
}

.custom-tabs ::v-deep .el-tabs__item.is-active {
  background-color: #0094de !important;
  color: white !important;
  border: none !important;
}

.custom-tabs ::v-deep .el-tabs__item:not(.is-active):hover {
  color: #409eff !important;
}

.bread-box {
  display: flex;
  align-items: center;
  justify-content: start;
}

.el-breadcrumb {
  padding: 15px;
  border-radius: 4px;
}

::v-deep .el-pagination.is-background .btn-next,
::v-deep .el-pagination.is-background .btn-prev,
::v-deep .el-pagination.is-background .el-pager li {
  background-color: #fff !important;
}

::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
  color: #596df4 !important;
  border: 1px solid #596df4 !important;
}
</style>